package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierEncashBatchDao;
import com.xb.loan.cashier.biz.dao.bean.CashierEncashBatch;
import com.xb.loan.cashier.biz.service.CashierEncashBatchService;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提现处理业务逻辑类
 */
@Service("cashierEncashBatchService")
public class CashierEncashBatchServiceImpl implements CashierEncashBatchService {
    
    @Autowired
    private CashierEncashBatchDao cashierEncashBatchDao;

    @Override
    public String getBatchId() {
        return FormatOperation.assemblySeq("encashBatch", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
    }
    
    /**
     * 更新提现批处理记录的信息
     * 
     * @author zheng.p
     * @date 2016年6月30日 下午4:20:34
     */
    @Override
    public int updateByPrimaryKeySelective(CashierEncashBatch cashierEncashBatch) {
        
        return cashierEncashBatchDao.updateByPrimaryKeySelective(cashierEncashBatch);
    }

    @Override
    public void insert(CashierEncashBatch cashierEncashBatch) {
        cashierEncashBatchDao.insert(cashierEncashBatch);
    }

    @Override
    public List<CashierEncashBatch> selectWaitBatchs() {
        return cashierEncashBatchDao.selectWaitBatchs();
    }

    private void updateToWaiting(String batchId) {
        cashierEncashBatchDao.updateToWaiting(batchId);
        // 操作多个sql需要添加事务
    }
    private void updateToWaitingCount(String batchId, Integer count) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("batchId", batchId);
    	map.put("count", count);
        cashierEncashBatchDao.updateToWaitingCount(map);
        // 操作多个sql需要添加事务
    }

    @Override
    public void updateToWaiting(List<CashierEncashBatch> batchs) {
        if(batchs==null || batchs.size()<1){
            return;
        }
        for(CashierEncashBatch batch : batchs){
        	if(batch.getCount() != 0){
        		updateToWaitingCount(batch.getBatchId(),batch.getCount());
        	}else{
        		updateToWaiting(batch.getBatchId());
        	}
        }
    }

    @Override
    public List<CashierEncashBatch> selectWaitingBatchs(int pageNo, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("from", (pageNo-1)*pageSize);
        params.put("size", pageSize);
        return cashierEncashBatchDao.selectWaitingBatchs(params);
    }

    @Override
    public int updateToSending(String batchId, String fileName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("batchId", batchId);
        params.put("fileName", fileName);
        return cashierEncashBatchDao.updateToSending(params);
    }
    
    @Override
    public int updateToFinish(String fileName) {
        return cashierEncashBatchDao.updateToFinish(fileName);
    }

    @Override
    public List<CashierEncashBatch> selecSendingBatchsByUpdateTime(Date updateTime, int pageNo, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("updateTime", updateTime);
        params.put("from", (pageNo-1)*pageSize);
        params.put("size", pageSize);
        return cashierEncashBatchDao.selecSendingBatchsByUpdateTime(params);
    }
}
